//
// cocos2d Hello World example
// http://www.cocos2d-iphone.org
//

// Import the interfaces
#import "MainScene.h"
#import "NumberGameScene.h"
#import "ColorGameScene.h"
#import "SimpleAudioEngine.h"
#import "OptionScene.h"
#import "HelpScene.h"

extern BOOL isSound;
extern BOOL isMusic;
extern BOOL isNormal;
extern int nNumberLevel;
extern int nColorLevel;

// HelloWorld implementation
@implementation MainScene

+(id) scene
{
	// 'scene' is an autorelease object.
	Scene *scene = [Scene node];
	
	// 'layer' is an autorelease object.
	MainScene *layer = [MainScene node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
		
		Sprite *background = [Sprite spriteWithFile:@"background.png"];
		[background setPosition:ccp(240, 160)];
		[self addChild:background z:0];
		
		// ask director the the window size
		CGSize size = [[Director sharedDirector] winSize];
		
		Sprite *sprite = [Sprite spriteWithFile:@"dog.png"];
		sprite.position = ccp( 0, 50);
		[self addChild:sprite];		
		id rotateAction = [RotateBy actionWithDuration:8 angle:180*4];		
		id jumpAction = [JumpBy actionWithDuration:8 position:ccp(size.width,0) height:200 jumps:4];		
		id fordward = [Spawn actions:rotateAction, jumpAction, nil];		
		id backwards = [fordward reverse];
		id sequence = [Sequence actions: fordward, backwards, nil];
		id repeat = [RepeatForever actionWithAction:sequence];
		[sprite runAction:repeat];	
		
		// create and initialize a Label
//		Label* label = [Label labelWithString:@"Dare Memory" fontName:@"Marker Felt" fontSize:64];
		BitmapFontAtlas* label = [BitmapFontAtlas bitmapFontAtlasWithString:@"Dare Memory" fntFile:@"bitmapFontTest.fnt"];
	
		// position the label on the center of the screen
		label.position =  ccp( size.width /2 , size.height/2 + 80);
		
		// add the label as a child to this Layer
		[self addChild: label];
		
		BitmapFontAtlas *labelNumber = [BitmapFontAtlas bitmapFontAtlasWithString:@"Start Number Game" fntFile:@"fort32.fnt"];
		MenuItem *itemNumber = [MenuItemLabel itemWithLabel:labelNumber target:self selector:@selector(StartNumberGame:)];
		BitmapFontAtlas *labelColor = [BitmapFontAtlas bitmapFontAtlasWithString:@"Start Color Game" fntFile:@"fort32.fnt"];
		MenuItem *itemColor = [MenuItemLabel itemWithLabel:labelColor target:self selector:@selector(StartColorGame:)];
		BitmapFontAtlas *labelReset = [BitmapFontAtlas bitmapFontAtlasWithString:@"Reset Game" fntFile:@"fort32.fnt"];
		MenuItem *itemReset = [MenuItemLabel itemWithLabel:labelReset target:self selector:@selector(ResetGame:)];
		BitmapFontAtlas *labelOption = [BitmapFontAtlas bitmapFontAtlasWithString:@"Options" fntFile:@"fort32.fnt"];
		MenuItem *itemOption = [MenuItemLabel itemWithLabel:labelOption target:self selector:@selector(GoOption:)];
		BitmapFontAtlas *labelHelp = [BitmapFontAtlas bitmapFontAtlasWithString:@"Help" fntFile:@"fort32.fnt"];
		MenuItem *itemHelp = [MenuItemLabel itemWithLabel:labelHelp target:self selector:@selector(GoHelp:)];
		Menu *menu = [Menu menuWithItems:itemNumber, itemColor, itemReset, itemOption, itemHelp, nil];
		[menu alignItemsVertically];
		menu.position = ccp( size.width/2, size.height/2 - 50);
		[self addChild:menu];
		[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"mymemory.mp3"];
		
		if (isMusic == YES) {
			if ([[SimpleAudioEngine sharedEngine] isBackgroundMusicPlaying] == NO) {
				[[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];
			}
			
		}
		else {
			[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];
		}	
	}
	return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

- (void) StartNumberGame: (id) sender
{
	[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[NumberGameScene node]]];
}

- (void) StartColorGame: (id) sender
{
	[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[ColorGameScene node]]];
}

- (void) GoOption: (id) sender
{
	[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[OptionScene node]]];
}

- (void) ResetGame: (id) sender
{
	nNumberLevel = 1;
	nColorLevel = 1;
}

- (void) GoHelp: (id) sender
{
	[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[HelpScene node]]];
}
@end
